

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include "input_manager.h"

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>



#define SERVER_PORT 8888

int g_iSocketServer;
static struct sockaddr_in g_iSocketServerAddr;
int iRecvLen;
char ucRecvBuf[1000];


 int Devicenetinit(void)
{

	int iRed;
	
	//调用socket得到句柄，无任何IP信息，SOCK_DGRAM（数据报）UDP连接
	g_iSocketServer =  socket(AF_INET , SOCK_DGRAM, 0);
	if(g_iSocketServer == -1)
	{
		printf("socket error\n");

		return -1;

	}

	g_iSocketServerAddr.sin_family	   =AF_INET;
	g_iSocketServerAddr.sin_port	= htons(SERVER_PORT);//把主机的字节序转换成网络字节序
	g_iSocketServerAddr.sin_addr.s_addr =INADDR_ANY;//检测本机上所有IP

	memset(g_iSocketServerAddr.sin_zero,0,8);
	
	//调用bind绑定IP和端口	
	iRed = bind(g_iSocketServer,(const struct sockaddr *)&g_iSocketServerAddr,sizeof(struct sockaddr));
	if(iRed == -1)
	{
		//出现此错误也可能是，退出服务器时没有等待一阵时间
		printf("bind error\n");
		return -1;

	}

	return 0;

}
int Devicenetexit(void)
{

	close(g_iSocketServer);
	return 0;

}



static int getinputevent(inputEvent_p inputEventp)
{
	unsigned int ClintAddr_len;
	ClintAddr_len =sizeof(struct sockaddr);


//获取数据
	iRecvLen =	recvfrom(g_iSocketServer, ucRecvBuf, 999, 0,(struct sockaddr *)&g_iSocketServerAddr,&ClintAddr_len);
	if(iRecvLen > 0)
	{
		ucRecvBuf[iRecvLen]='\0';
	
	}


	inputEventp->type =type_net_event;
	strncpy(inputEventp->net_str, ucRecvBuf,1000); 
	inputEventp->net_str[999] = '\0';
	gettimeofday(&inputEventp->timevalue, NULL);

	
	return 0;

}


 struct input_Device netinput = {
 
 	.name = "net",
	.GetInputEvent=getinputevent,
	.DeviceInit=Devicenetinit,
	.DeviceExit=Devicenetexit,


};


void SocketNetRegsiter(void)
{

//注册input_Device
  RegsiterInput(&netinput);
//wedinit



}























